#!/bin/sh

# Copyright (C) 2011-2020 Internet Systems Consortium, Inc. ("ISC")
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.

# Checks that the logger will route messages to the chosen destination.

# Exit with error if commands exit with non-zero and if undefined variables are
# used.
set -eu

# Include common test library.
# shellcheck disable=SC1091
# SC1091: Not following: ... was not specified as input (see shellcheck -x).
. "@abs_top_builddir@/src/lib/testutils/dhcp_test_lib.sh"

tempfile="@abs_builddir@/destination_test_tempfile_$$"
destfile1_tmp="@abs_builddir@/destination_test_destfile_1_tmp_$$"
destfile2_tmp="@abs_builddir@/destination_test_destfile_2_tmp_$$"
destfile1="@abs_builddir@/destination_test_destfile_1_$$"
destfile2="@abs_builddir@/destination_test_destfile_2_$$"

printf '1. One logger, multiple destinations:\n'

cat > $tempfile << .
FATAL [example] LOG_WRITE_ERROR error writing to test1: 42
ERROR [example] LOG_READING_LOCAL_FILE reading local message file dummy/file
FATAL [example.beta] LOG_BAD_SEVERITY unrecognized log severity: beta_fatal
ERROR [example.beta] LOG_BAD_DESTINATION unrecognized log destination: beta_error
.
rm -f $destfile1 $destfile2
./logger_example -s error -f $destfile1_tmp -f $destfile2_tmp

# strip the pids and thread ids
sed -e 's/\[\([a-z0-9\.]\{1,\}\)\/\([0-9]\{1,\}\)\.\(0x\)\{0,1\}\([0-9A-Fa-f]\{1,\}\)\]/[\1]/' < $destfile1_tmp > $destfile1
sed -e 's/\[\([a-z0-9\.]\{1,\}\)\/\([0-9]\{1,\}\)\.\(0x\)\{0,1\}\([0-9A-Fa-f]\{1,\}\)\]/[\1]/' < $destfile2_tmp > $destfile2
# strip the thread ids

test_start 'logger-destination.one-logger-file-1'
cut -d' ' -f3- $destfile1 | diff $tempfile -
test_finish $?

test_start 'logger-destination.one-logger-file-2'
cut -d' ' -f3- $destfile2 | diff $tempfile -
test_finish $?

# Tidy up.
rm -f $tempfile $destfile1_tmp $destfile2_tmp $destfile1 $destfile2

printf '2. Two loggers, different destinations and severities:\n'
rm -f $destfile1 $destfile2
./logger_example -l example -s info -f $destfile1_tmp -l alpha -s warn -f $destfile2_tmp

# strip the pids and thread ids
sed -e 's/\[\([a-z0-9\.]\{1,\}\)\/\([0-9]\{1,\}\)\.\(0x\)\{0,1\}\([0-9A-Fa-f]\{1,\}\)\]/[\1]/' < $destfile1_tmp > $destfile1
sed -e 's/\[\([a-z0-9\.]\{1,\}\)\/\([0-9]\{1,\}\)\.\(0x\)\{0,1\}\([0-9A-Fa-f]\{1,\}\)\]/[\1]/' < $destfile2_tmp > $destfile2

# All output for example and example.beta should have gone to destfile1.
# Output for example.alpha should have done to destfile2.

test_start 'logger-destination.multiples-loggers-file-1'
cat > $tempfile << .
FATAL [example] LOG_WRITE_ERROR error writing to test1: 42
ERROR [example] LOG_READING_LOCAL_FILE reading local message file dummy/file
WARN  [example] LOG_BAD_STREAM bad log console output stream: example
FATAL [example.beta] LOG_BAD_SEVERITY unrecognized log severity: beta_fatal
ERROR [example.beta] LOG_BAD_DESTINATION unrecognized log destination: beta_error
WARN  [example.beta] LOG_BAD_STREAM bad log console output stream: beta_warn
INFO  [example.beta] LOG_READ_ERROR error reading from message file beta: info
.
cut -d' ' -f3- $destfile1 | diff $tempfile -
test_finish $?

test_start 'logger-destination.multiples-loggers-file-2'
cat > $tempfile << .
WARN  [example.alpha] LOG_READ_ERROR error reading from message file a.txt: dummy reason
.
cut -d' ' -f3- $destfile2 | diff $tempfile -
test_finish $?

# Tidy up.
rm -f $tempfile $destfile1_tmp $destfile2_tmp $destfile1 $destfile2
